Page({
  data: {
    collections: []
  },

  onLoad: function() {
    this.loadCollections();
  },

  onShow: function() {
    this.loadCollections();
  },

  loadCollections: function() {
    const collections = wx.getStorageSync('userCollections') || [];
    this.setData({
      collections: collections
    });
  },

  removeCollection: function(e) {
    const id = e.currentTarget.dataset.id;
    const that = this;
    
    wx.showModal({
      title: '提示',
      content: '确定要取消收藏吗？',
      success: function(res) {
        if (res.confirm) {
          let collections = wx.getStorageSync('userCollections') || [];
          collections = collections.filter(item => item.id !== id);
          wx.setStorageSync('userCollections', collections);
          
          that.setData({
            collections: collections
          });
          
          // 更新个人中心的收藏数量
          that.updateCollectionCount(collections.length);
          
          wx.showToast({
            title: '已取消收藏',
            icon: 'success'
          });
        }
      }
    });
  },

  updateCollectionCount: function(count) {
    const userStats = wx.getStorageSync('userStats') || {
      collectionCount: 0,
      footprintCount: 0,
      points: 0
    };
    userStats.collectionCount = count;
    wx.setStorageSync('userStats', userStats);
  },

  goToIndex: function() {
    wx.switchTab({
      url: '/pages/index/index'
    });
  }
})